package com.caszx.chargingpile.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONException;
import com.app.base.adapter.BaseHeadFootAdapter;
import com.app.base.app.BaseTopBarNetActivity;
import com.app.jsonparse.JSONParse;
import com.app.utils.DiskUser;
import com.app.utils.DividerGridItemDecoration;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.entity.EntityChargeRecord;
import com.caszx.chargingpile.entity.EntityCommPile;
import com.caszx.chargingpile.entity.EntityStation;
import com.caszx.chargingpile.holder.reservate.OftenUseHolder;
import com.caszx.chargingpile.holder.user.ChargeRecordHolder;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.user.LocationManager;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.Call;

/**
 * 充值记录
 * */
public class UserChargeRecordActivity extends BaseTopBarNetActivity {

    private RecyclerView mRecyclerView;
    private BaseHeadFootAdapter<EntityChargeRecord> mRecyclerAdapter;
    private PtrClassicFrameLayout ptrFrame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCenterTitle("充值记录");
    }

    @Override
    protected void onLoad() {
        cancelAll(this);
        HashMap<String,String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(this,"token"));
        OkHttpUtils.post()
                .url(ConstantUrl.URL_USER_CHARGE_RECORD)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                      if(ptrFrame!=null){
                          ptrFrame.refreshComplete();
                      }
                        showErrorView(R.string.error_network);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        if(ptrFrame!=null){
                            ptrFrame.refreshComplete();
                        }
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                showErrorView(resultJsonObject.optString("msg"));
                            }else{
                                try {
                                    List<EntityChargeRecord> datas = JSONParse.parseArray(response.optString("rechargeRecord"),EntityChargeRecord.class);
                                    showContentView();
                                    mRecyclerAdapter.setDatas(datas);
                                }catch (JSONException e){
                                    showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected View initOnloadedView() {
        View view =getLayoutInflater().inflate( R.layout.common_refrash_recyclerview, null);
        initPtrFrameLayout(view);
        initRecyclerView(view);
        return view;
    }


    private void initPtrFrameLayout(View view) {
        ptrFrame = (PtrClassicFrameLayout) view .findViewById(R.id.ptr_classic_frameLayout);
        ptrFrame.setLastUpdateTimeRelateObject(this);
        ptrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                onLoad();
            }
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame,
                                             View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(ptrFrame,
                        mRecyclerView, header);
            }
        });
    }

    private void initRecyclerView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setBackgroundResource(R.color.gray3);
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        DividerGridItemDecoration itemDecoration = new DividerGridItemDecoration(this);
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerAdapter = new BaseHeadFootAdapter<>(R.layout.item_charge_record,ChargeRecordHolder.class);
        mRecyclerView.setAdapter(mRecyclerAdapter);

    }
}
